package com.tfswx.fljdmxyy.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.newbie.core.exception.BusinessException;
import com.tfswx.fljdmxyy.constant.SystemConstant;
import com.tfswx.fljdmxyy.dao.QxXtGnflMapper;
import com.tfswx.fljdmxyy.dto.gnfl.*;
import com.tfswx.fljdmxyy.dto.rygl.RyxxOutputDTO;
import com.tfswx.fljdmxyy.enums.BusinessCodeEnum;
import com.tfswx.fljdmxyy.model.QxXtGnfl;
import com.tfswx.fljdmxyy.query.gnfl.GnQueryResult;
import com.tfswx.fljdmxyy.query.gnfl.GnflQueryParam;
import com.tfswx.fljdmxyy.query.gnfl.GnflQueryResult;
import com.tfswx.fljdmxyy.query.gnfl.GnxxQueryResult;
import com.tfswx.fljdmxyy.service.GnflService;
import com.tfswx.fljdmxyy.service.RyglService;
import com.tfswx.fljdmxyy.service.WjfwService;
import com.tfswx.fljdmxyy.utils.ConverterUtils;
import com.tfswx.fljdmxyy.utils.UuidUtils;
import com.tfswx.newbie.boot.starter.page.PageHelper;
import com.tfswx.newbie.boot.starter.page.PageParam;
import com.tfswx.newbie.boot.starter.page.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 功能分类业务实现类
 *
 * @author: 陈文佳
 * @date: 2022-10-13
 **/
@Slf4j
@Service
public class GnflServiceImpl implements GnflService {

    @Resource
    private QxXtGnflMapper qxXtGnflMapper;

    @Resource
    private RyglService ryglService;

    @Resource
    private WjfwService wjfwService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GnflAddOutputDTO addGnfl(GnflAddInputDTO inputDTO) {
        // 1.获取用户信息
        RyxxOutputDTO ryxxOutputDTO = ryglService.getUserInfo();
        if (ObjectUtil.isNull(ryxxOutputDTO)) {
            throw new BusinessException(BusinessCodeEnum.HQSJSB);
        }

        // 2.设置功能分类属性
        QxXtGnfl gnflParam = ConverterUtils.convert(inputDTO, QxXtGnfl.class);
        gnflParam.setFlbm(UuidUtils.uuid32());
        Date now = new Date();
        gnflParam.setCjsj(now);
        gnflParam.setZhxgsj(now);
        gnflParam.setCjrbm(ryxxOutputDTO.getRybm());
        gnflParam.setCjrmc(ryxxOutputDTO.getRymc());
        gnflParam.setZhxgrbm(ryxxOutputDTO.getRybm());
        gnflParam.setZhxgrmc(ryxxOutputDTO.getRymc());

        // 3.获取分类序号
        if (ObjectUtil.isNull(gnflParam.getFlxh())) {
            Integer gnflMaxXh = qxXtGnflMapper.getGnflMaxXh(gnflParam);
            if (ObjectUtil.isNull(gnflMaxXh)) {
                gnflMaxXh = 0;
            }
            gnflParam.setFlxh(gnflMaxXh + 1);
        }

        // 4.保存功能分类
        int res = qxXtGnflMapper.insertGnfl(gnflParam);
        if (res < 1) {
            throw new BusinessException(BusinessCodeEnum.TJSJSB);
        }

        // 5.如果传入了图标，则确认分类图标文件
        String fltb = gnflParam.getFltb();
        if (StrUtil.isNotBlank(fltb)) {
            wjfwService.confirmFile(SystemConstant.ZYLXBM_GNFL, gnflParam.getFlbm(), gnflParam.getFltb());
        }
        return ConverterUtils.convert(gnflParam, GnflAddOutputDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateGnfl(GnflUpdateInputDTO inputDTO) {
        // 1.获取用户信息
        RyxxOutputDTO ryxxOutputDTO = ryglService.getUserInfo();
        if (ObjectUtil.isNull(ryxxOutputDTO)) {
            throw new BusinessException(BusinessCodeEnum.HQSJSB);
        }

        // 2.查询修改前的功能分类
        QxXtGnfl gnflParam = ConverterUtils.convert(inputDTO, QxXtGnfl.class);
        QxXtGnfl oldGnfl = qxXtGnflMapper.getGnfl(gnflParam);
        if (ObjectUtil.isNull(oldGnfl)) {
            throw new BusinessException(BusinessCodeEnum.XGSJSB);
        }
        gnflParam.setZhxgsj(new Date());
        gnflParam.setZhxgrbm(ryxxOutputDTO.getRybm());
        gnflParam.setZhxgrmc(ryxxOutputDTO.getRymc());

        // 3 修改功能分类
        int res = qxXtGnflMapper.updateGnfl(gnflParam);
        if (res < 1) {
            throw new BusinessException(BusinessCodeEnum.XGSB);
        }

        // 4.如果没有传入图标 则删除图标
        if (StrUtil.isBlank(gnflParam.getFltb())) {
            wjfwService.deleteFile(SystemConstant.ZYLXBM_GNFL, gnflParam.getFlbm());
        } else {
            // 5. 如果传入了新的图标 则删除旧图标 确认新图标 反之 不做操作
            if (!StrUtil.equals(oldGnfl.getFltb(), gnflParam.getFltb())) {
                wjfwService.deleteFile(SystemConstant.ZYLXBM_GNFL, gnflParam.getFlbm());
                wjfwService.confirmFile(SystemConstant.ZYLXBM_GNFL, gnflParam.getFlbm(), gnflParam.getFltb());
            }
        }
        return Boolean.TRUE;
    }

    @Override
    public GnflGetOutputDTO getGnfl(GnflGetInputDTO inputDTO) {
        QxXtGnfl gnflParam = ConverterUtils.convert(inputDTO, QxXtGnfl.class);
        return ConverterUtils.convert(qxXtGnflMapper.getGnfl(gnflParam), GnflGetOutputDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteGnfl(GnflDeleteInputDTO inputDTO) {
        inputDTO.getFlbm().forEach(flbm -> {
            QxXtGnfl gnflParam = new QxXtGnfl();
            // 1.查询删除前的功能分类
            gnflParam.setFlbm(flbm);
            gnflParam.setZhxgsj(new Date());
            QxXtGnfl gnfl = qxXtGnflMapper.getGnfl(gnflParam);
            if (ObjectUtil.isNull(gnfl)) {
                throw new BusinessException(BusinessCodeEnum.SCSJSB);
            }

            // 2.删除功能分类
            int res = qxXtGnflMapper.deleteGnfl(gnflParam);
            if (res < 1) {
                throw new BusinessException(BusinessCodeEnum.SCSB);
            }

            // 3.存在图标 则删除图标
            if (StrUtil.isNotBlank(gnfl.getFltb())) {
                wjfwService.deleteFile(SystemConstant.ZYLXBM_GNFL, gnflParam.getFlbm());
            }
        });
        return Boolean.TRUE;
    }

    @Override
    public PageResult<GnflQueryResult> queryGnfl(PageParam<GnflQueryParam> param) {
        return PageHelper.doSelectPage(param, () -> qxXtGnflMapper.queryGnfl(param.getFilter()));
    }

    @Override
    public List<GnQueryResult> queryGnlb() {
        return qxXtGnflMapper.queryGnlb();
    }

    @Override
    public List<GnxxQueryResult> queryGnxx() {
        return qxXtGnflMapper.queryGnxx();
    }
}